home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 11130 / Blender249[Riesen][PC][_xmac][_xmot][2016-06-27].7z / Blender249[Riesen][PC][_xmac][_xmot][2016-06-27] / starter.py < prev   
Encoding:
Python Source  |  2016-06-27  |  5.8 KB  |  270 lines

  1. import newGameLib
  2. from newGameLib import *
  3. import Blender    
  4.  
  5. def getImagePath(filename,tex):
  6.  
  7.     imagePath=None    
  8.     imageDir=filename.lower().split('animations')[0]+'images'
  9.     if os.path.exists(imageDir)==True:
  10.         search=Searcher()
  11.         search.dir=imageDir
  12.         search.what=tex
  13.         search.run()
  14.         list=search.list
  15.         if len(list)>0:
  16.             imagePath=list[0]
  17.             sys=Sys(imagePath)
  18.             if sys.ext=='_ximg':
  19.                 file=open(imagePath,'rb')
  20.                 p=BinaryReader(file)
  21.                 p.seek(265)
  22.                 imagePath=imagePath.replace('._ximg','.dds')
  23.                 new=open(imagePath,'wb')
  24.                 new.write(p.read(p.fileSize()-265))
  25.                 new.close()
  26.     return imagePath
  27.  
  28. def _xmacParser(filename,g):
  29.     g.logOpen()
  30.     g.word(8)
  31.     g.seek(146)
  32.     if g.B(1)[0]==1:
  33.         g.endian='>'
  34.     else:
  35.         g.endian='<'
  36.     g.seek(176)
  37.     g.word(g.i(1)[0])
  38.     g.word(g.i(1)[0])
  39.     g.word(g.i(1)[0])
  40.     
  41.     ############    
  42.     #bones    
  43.     ############
  44.     w=g.i(6)
  45.     skeleton=Skeleton()
  46.     skeleton.NICE=True
  47.     skeleton.BONESPACE=True
  48.     for i in range(w[4]):
  49.         bone=Bone()
  50.         bone.rotMatrix=QuatMatrix(g.f(4))
  51.         g.f(4)
  52.         bone.posMatrix=VectorMatrix(g.f(3))
  53.         g.f(3)
  54.         g.f(3)
  55.         v=g.i(4)
  56.         if v[2]!=-1:
  57.             bone.parentID=v[2]
  58.         g.B(4)
  59.         g.f(4)
  60.         g.f(4)
  61.         g.f(4)
  62.         g.f(4)
  63.         g.f(1)
  64.         bone.name=g.word(g.i(1)[0])[-25:]
  65.         skeleton.boneList.append(bone)
  66.     skeleton.draw()
  67.     
  68.     
  69.     ############    
  70.     #materials
  71.     ############
  72.     w=g.i(6)
  73.     matList=[]
  74.     for m in range(w[3]):#material list
  75.         mat=Mat()
  76.         mat.TRIANGLE=True
  77.         g.i(3)
  78.         g.f(20)
  79.         chunk=g.i(1)[0]    
  80.         if chunk==4587520:
  81.             g.word(g.i(1)[0])
  82.         elif chunk==21364736:    
  83.             g.word(g.i(1)[0])
  84.             g.f(6)
  85.             g.H(2)    
  86.             tex=g.word(g.i(1)[0]).lower()
  87.             mat.diffuse=getImagePath(filename,tex)
  88.                     
  89.         elif chunk==38141952:    
  90.             g.word(g.i(1)[0])
  91.             g.f(6)
  92.             g.H(2)    
  93.             tex=g.word(g.i(1)[0]).lower()
  94.             mat.diffuse=getImagePath(filename,tex)
  95.             g.f(6)
  96.             g.H(2)    
  97.             tex=g.word(g.i(1)[0]).lower()
  98.             mat.normal=getImagePath(filename,tex)
  99.                     
  100.         elif chunk==17922:    
  101.             g.word(g.i(1)[0])
  102.             g.f(6)
  103.             g.H(2)    
  104.             tex=g.word(g.i(1)[0]).lower()
  105.             mat.diffuse=getImagePath(filename,tex)
  106.             g.f(6)
  107.             g.H(2)    
  108.             tex=g.word(g.i(1)[0]).lower()
  109.             mat.normal=getImagePath(filename,tex)
  110.         elif chunk==54919168:    
  111.             g.word(g.i(1)[0])
  112.             g.f(6)
  113.             g.H(2)    
  114.             tex=g.word(g.i(1)[0]).lower()
  115.             mat.diffuse=getImagePath(filename,tex)
  116.             g.f(6)
  117.             g.H(2)    
  118.             tex=g.word(g.i(1)[0]).lower()
  119.             mat.normal=getImagePath(filename,tex)
  120.             g.f(6)
  121.             g.H(2)    
  122.             tex=g.word(g.i(1)[0]).lower()
  123.             mat.specular=getImagePath(filename,tex)
  124.         elif chunk==17923:    
  125.             g.word(g.i(1)[0])
  126.             g.f(6)
  127.             g.H(2)    
  128.             tex=g.word(g.i(1)[0]).lower()
  129.             mat.diffuse=getImagePath(filename,tex)
  130.             g.f(6)
  131.             g.H(2)    
  132.             tex=g.word(g.i(1)[0]).lower()
  133.             mat.normal=getImagePath(filename,tex)
  134.             g.f(6)
  135.             g.H(2)    
  136.             tex=g.word(g.i(1)[0]).lower()
  137.             mat.specular=getImagePath(filename,tex)
  138.         else:
  139.             print 'WARNING:',chunk
  140.         matList.append(mat)    
  141.     g.logClose()    
  142.     
  143.     
  144.     ############    
  145.     #meshes
  146.     ############
  147.     for j in range(2):#lod list L1,L2 - delete break if want all lods
  148.         w=g.i(10)
  149.         count=w[4]
  150.         vertPosList=[]
  151.         vertUVList=[]
  152.         skinIDList=[]
  153.         for i in range(w[8]):
  154.             section=g.i(3)    
  155.             for m in range(w[5]):
  156.                 ts=g.tell()
  157.                 
  158.                 if section[0]==0:vertPosList.append(g.f(3))
  159.                 if section[0]==3:vertUVList.append(g.f(2))
  160.                 if section[0]==5:skinIDList.append(g.i(1)[0])
  161.                 if section[0]==2:g.f(4)
  162.                 
  163.                 g.seek(ts+section[1])
  164.                 
  165.         vertIDStart=0
  166.         meshList=[]
  167.         subMeshInfoList=[]
  168.         subMeshCount=w[7]
  169.         for i in range(subMeshCount):
  170.             mesh=Mesh()
  171.             mesh.TRIANGLE=True    
  172.             mesh.BINDSKELETON=skeleton.name    
  173.             v=g.i(4)
  174.             mesh.matList.append(matList[v[2]])
  175.             subMeshInfoList.append(v)
  176.             mesh.indiceList=list(g.i(v[0]))
  177.             mesh.vertPosList=vertPosList[vertIDStart:vertIDStart+v[1]]
  178.             mesh.vertUVList=vertUVList[vertIDStart:vertIDStart+v[1]]
  179.             boneMap=g.i(v[3])
  180.             skin=Skin()
  181.             vertIDStart+=v[1]
  182.             skin.boneMap=skeleton.boneNameList
  183.             mesh.skinList.append(skin)
  184.             meshList.append(mesh)
  185.             
  186.         w=g.i(7)
  187.         listA=[]
  188.         for i in range(w[5]):listA.append([g.f(1)[0],g.B(4)])
  189.         listB=[]#skin list    
  190.         ID=0
  191.         for i in range(count):
  192.             v=g.i(2)
  193.             indiceList=[]
  194.             weightList=[]
  195.             for m in range(v[1]):
  196.                 indiceList.append(listA[ID][1][0])
  197.                 weightList.append(listA[ID][0])
  198.                 ID+=1
  199.             listB.append([indiceList,weightList])
  200.         
  201.         vertIDStart=0
  202.         for i in range(subMeshCount):
  203.             mesh=meshList[i]
  204.             for m in range(vertIDStart,vertIDStart+subMeshInfoList[i][1]):
  205.                 mesh.skinIndiceList.append(listB[skinIDList[m]][0])
  206.                 mesh.skinWeightList.append(listB[skinIDList[m]][1])
  207.             vertIDStart+=subMeshInfoList[i][1]
  208.             mesh.draw()
  209.             
  210.         
  211.         
  212.         break    
  213.  
  214. def _xmotParser(filename,g):
  215.  
  216.     action=Action()
  217.     action.BONESPACE=True
  218.     action.BONESORT=True
  219.     
  220.     try:
  221.         g.find('Bone_ROOT')
  222.         g.seek(-145,1)
  223.         while(True):
  224.             if g.tell()>=g.fileSize():break
  225.             for i in range(4):
  226.                 g.f(4)
  227.             for i in range(2):
  228.                 g.f(3)
  229.                 g.f(3)
  230.                 
  231.             countList=g.i(4)
  232.             time=g.f(1)    
  233.             
  234.             bone=ActionBone()
  235.             bone.name=g.word(g.i(1)[0])
  236.             for i in range(countList[0]):
  237.                 bone.posKeyList.append(VectorMatrix(g.f(3)))
  238.                 bone.posFrameList.append(int(g.f(1)[0]*30))
  239.             for i in range(countList[1]):
  240.                 bone.rotKeyList.append(QuatMatrix(g.f(4)).resize4x4())
  241.                 bone.rotFrameList.append(int(g.f(1)[0]*30))
  242.             for i in range(countList[3]):g.f(5)
  243.             for i in range(countList[2]):g.f(4)
  244.             action.boneList.append(bone)
  245.         action.draw()
  246.         action.setContext()    
  247.     except:
  248.         pass
  249.         
  250.     
  251. def Parser(filename):    
  252.     print filename
  253.     ext=filename.split('.')[-1].lower()    
  254.     
  255.     if ext=='_xmac':
  256.         file=open(filename,'rb')
  257.         g=BinaryReader(file)
  258.         _xmacParser(filename,g)
  259.         file.close()
  260.     
  261.     if ext=='_xmot':
  262.         file=open(filename,'rb')
  263.         g=BinaryReader(file)
  264.         _xmotParser(filename,g)
  265.         file.close()
  266.  
  267.  
  268.     
  269. Blender.Window.FileSelector(Parser,'import','select: _xmac,_xmot') 
  270.